﻿using System;
using System.Threading;
using System.Xml;
using System.Xml.Serialization;
using CS.TaskScheduling;
using CS.Util.Text;
using NUnit.Framework;

namespace CS.Scheduling.Test.Temp
{
    /// <summary>
    /// xml配置的相关测试
    /// </summary>
    [TestFixture]
    public class XmlConfigSomeTest
    {
        /// <summary>
        /// 创建一个新的Xml配置文件
        /// 通过序列化生成XML配置文件，快速准确
        /// </summary>
        [Test]
        public void CreateNewConfig()
        {
            //var config = TaskConfig.GetInstance();
            ////foreach (var task in config.Tasks)
            ////{
            ////    Console.WriteLine(task.Job.Name);
            ////}
            //Console.WriteLine(config.WatchTimer.DelayMillisecond);

            //Console.WriteLine(config.ToXml());
        }

        /// <summary>
        /// 创建执行情况xml片断
        /// </summary>
        [Test]
        public void CreateNewExecutionConfigTest()
        {
            var c1 = new ExecutionConfig();
            //c1.Jobs.Add(new JobInfo { Id = 4475, Name = "te45544st", Execution = new ExecutionInfo() });


            var job = new JobInfo {Id = "45", Name = "test", Execution = new ExecutionInfo()};
            c1.Jobs.Add(job);

            foreach (JobInfo jobs in c1.Jobs)
            {
                Console.WriteLine(job.Name);
            }

            //var r1 = Serializer.XmlSerializerToText(c1);
            //Console.WriteLine(r1);

            c1.Save();
        }

        [Test]
        public void DeXml_Test()
        {
            //var job = new JobInfo() {Id = "145", Name = "thisaetwet"};

            string xml = "";
            string file = @"E:\Document\My Projects\CSWave Project\NUint.Test\bin\Tasks.Execution.xml";
            //using (var xmlReader = new XmlTextReader(file))
            //{
            //    Console.WriteLine(xmlReader.Value);
            //}

            using (XmlReader reader = XmlReader.Create(file))
            {
                var slz = new XmlSerializer(typeof (ExecutionConfig));
                var exec = slz.Deserialize(reader) as ExecutionConfig;
                reader.Close();
                Console.WriteLine(exec.Jobs.Count);
            }
        }

        [Test]
        public void InitConfigAndLoadConfigInfor_Test()
        {
            //var config = TaskConfig.GetInstance();

            ////  config.Tasks[0].Job.Execution.ErrorRunTimes ++;

            //foreach (var task in config.Tasks)
            //{
            //    Console.WriteLine(task.Job.Execution.SleepTimes);
            //    task.Job.Execution.RunTimes++;
            //    task.CalculateLaunchTime();
            //    //task.Job.Execution.LastRun = DateTime.Now;
            //    //Console.WriteLine("Run Once");
            //    Console.WriteLine(task.Job.Execution.RunTimes);

            //}


            //config.Execution.Save();


            //Console.WriteLine(config.Tasks.Count);
        }

        [Test]
        public void ReadConfigTest()
        {
            //var config = new ConfigExecutionInfo();
            //config = config.Read() as ConfigExecutionInfo;
            // Console.WriteLine(config.Jobs[0].Job.Name);
            var config =
                Serializer.XmlDeserializerFormFile<ExecutionConfig>(
                    @"E:\Document\My Projects\CSWave Project\NUint.Test\bin\Tasks.Execution.xml");
            Console.WriteLine(config.Jobs[0].Name);
        }

        [Test]
        public void Temp_Test()
        {
            DateTime now = SystemTime.Now();
            Console.WriteLine(now.TimeOfDay);
        }

        /// <summary>
        /// 写入时间间隔异常测试
        /// </summary>
        [Test]
        public void WriteConfigExceptionInterval_Test()
        {
            for (int i = 0; i < 4; i++)
            {
                InitConfigAndLoadConfigInfor_Test();
                Thread.Sleep(5000);
            }
        }
    }
}